//
//  AddViewControl.m
//  NoteDemo
//
//  Created by 郑火 on 16/2/17.
//  Copyright © 2016年 zwz. All rights reserved.
//

#import "AddViewControl.h"

@implementation AddViewControl

- (IBAction)onClickCancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)onClickSave:(id)sender {
    
    Note *note = [Note new];
    
    NSDate *date = [[NSDate alloc] init];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString* strDate  = [dateFormatter stringFromDate:date];
    
    note.date = strDate;
    note.content = self.txtView.text;
    self.nbl.delegate = self;
    [self.nbl createNote: note];
    
    [self.txtView resignFirstResponder];
}


//插入Note方法 成功
- (void)createNoteFinished
{
//    UIAlertController *alertView =[UIAlertController  alertControllerWithTitle:@"操作信息" message:@"插入成功。" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil];
//    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
//    [alertView addAction:cancelAction];
//    [alertView addAction:okAction];
//    [self presentViewController:alertView animated:YES completion:nil];
//    [self dismissViewControllerAnimated:YES completion:nil];
    UIAlertController *uac = [UIAlertController alertControllerWithTitle:@"操作信息" message:@"新建成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [uac addAction:okAction];
    [self presentViewController:uac animated:YES completion:nil];
}

//插入Note方法 失败
- (void)createNoteFailed:(NSError *)error
{
    
    NSString *errorStr = [error localizedDescription];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作信息"
//                                                        message:errorStr
//                                                       delegate:self
//                                              cancelButtonTitle:@"返回"
//                                              otherButtonTitles:@"继续", nil];
//    [alertView show];
//    
    UIAlertController *uac = [UIAlertController alertControllerWithTitle:@"操作信息" message:errorStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [uac addAction:okAction];
    [self presentViewController:uac animated:YES completion:nil];
    
}

//响应对话框按钮事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {//选择返回按钮
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if (buttonIndex == 1) {
        self.txtView.text = @"";
        [self.txtView becomeFirstResponder];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.nbl = [[NoteBuinessLogic alloc]init];
    self.nbl.delegate = self;
    [self.txtView becomeFirstResponder];
}

@end
